<?php

class FRM_Tools_Image {
	static function Resize($src, $dest, $width = 120, $height = 90, $watermark = false) {
		
		$rgb = 0xFFFFFF;
		$quality = 100;
		
		if (! file_exists ( $src ))
			return false;
		$size = getimagesize ( $src );
		if ($size === false)
			return false;
		$format = strtolower ( substr ( $size ['mime'], strpos ( $size ['mime'], '/' ) + 1 ) );
		$icfunc = "imagecreatefrom" . $format;
		if (! function_exists ( $icfunc ))
			return false;
		
		$x_ratio = $size [0] / $width;
		$y_ratio = $size [1] / $height;
		$ratio = min ( $x_ratio, $y_ratio );
		
		$use_x_ratio = ($x_ratio == $ratio);
		$use_y_ratio = ($y_ratio == $ratio);
		
		$div_x = $use_x_ratio ? 0 : floor ( ($size [0] - $width * $ratio) / 2 );
		$div_y = $use_y_ratio ? 0 : floor ( ($size [1] - $height * $ratio) / 2 );
		
		$isrc = $icfunc ( $src );
		
		$idest = imagecreatetruecolor ( $width, $height );
		imagefill ( $idest, 0, 0, $rgb );
		imagecopyresampled ( $idest, $isrc, 0, 0, $div_x, $div_y, $width, $height, $size [0] - 2 * $div_x, $size [1] - 2 * $div_y );
		
		if ($watermark === true) {
			$wm_image = imagecreatefrompng ( 'application/views/scripts/client/images/watermark.png' );
			imagecopyresampled ( $idest, $wm_image, $width - 135, $height - 20, 0, 0, 130, 20, 130, 20 );
			var_dump ( TEMP_IMG . 'watermark.png' );
			var_dump ( $wm_image );
		}
		
		imagejpeg ( $idest, $dest, $quality );
		imagedestroy ( $isrc );
		imagedestroy ( $idest );
		return true;
	}
	
	/*
	 * Commenting by Ruslan.
	 * 
	static function Resize($src, $dest, $width=120, $height=90, $watermark=false) {
		$rgb=0xFFFFFF;
		$quality=70;

		if (!file_exists($src)) return false;

		$size = getimagesize($src);
		if ($size === false) return false;

		$format = strtolower(substr($size['mime'], strpos($size['mime'], '/')+1));
		$icfunc = "imagecreatefrom" . $format;
		if (!function_exists($icfunc)) return false;

		$x_ratio = $width / $size[0];
		$y_ratio = $height / $size[1];

		$ratio = min($x_ratio, $y_ratio);
		$use_x_ratio = ($x_ratio == $ratio);
		$isrc = $icfunc($src);

		$idest = imagecreatetruecolor($width, $height);
		imagefill($idest, 0, 0, $rgb);

		if (($size[0] < $width) or ($size[1] < $height)) {

			$new_left = floor(($width - $size[0]) / 2);
			$new_top = floor(($height - $size[1]) / 2);

			imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0, $size[0], $size[1], $size[0], $size[1]);

		} else {

			$new_width = $use_x_ratio ? $width : ceil($size[0] * $ratio);
			$new_height = !$use_x_ratio ? $height : ceil($size[1] * $ratio);
			$new_left = $use_x_ratio ? 0 : floor(($width - $new_width) / 2);
			$new_top = !$use_x_ratio ? 0 : floor(($height - $new_height) / 2);

			imagecopyresampled($idest, $isrc, $new_left, $new_top, 0, 0, $new_width, $new_height, $size[0], $size[1]);
		}
		
		if ($watermark === true)
		{
			$wm_image = imagecreatefrompng('application/views/scripts/client/images/watermark.png');
			imagecopyresampled($idest, $wm_image, $width-135, $height-20, 0, 0, 130, 20, 130, 20);
			var_dump(TEMP_IMG . 'watermark.png');
			var_dump($wm_image);
		}

		imagejpeg($idest, $dest, $quality);
		imagedestroy($isrc);
		imagedestroy($idest);
		return $size;
	}
	*/
	static function Layer($src, $mask, $quality = 100) {
		if (file_exists ( $src )) {
			$size_img = getimagesize ( $src );
			
			if ($size_img [2] == 2)
				$img = imagecreatefromjpeg ( $src );
			else if ($size_img [2] == 1)
				$img = imagecreatefromgif ( $src );
			else if ($size_img [2] == 3)
				$img = imagecreatefrompng ( $src );
			
			$size_mask = getimagesize ( $mask );
			if ($size_mask [2] != 3)
				return false;
			
			$blank = imagecreatefrompng ( $mask );
			$create_mask = imagecreatefrompng ( $mask );
			
			imagealphablending ( $blank, true );
			
			ImageCopy ( $blank, $img, 0, 0, 0, 0, $size_img [0], $size_img [1] );
			ImageCopy ( $blank, $create_mask, 0, 0, 0, 0, $size_img [0], $size_img [1] );
			
			if ($size_img [2] == 2)
				imagejpeg ( $blank, $src, $quality );
			else if ($size_img [2] == 1)
				$img = imagecreatefromgif ( $blank, $src, $quality );
			else if ($size_img [2] == 3)
				$img = imagecreatefrompng ( $blank, $src, $quality );
			
			imagedestroy ( $blank );
			imagedestroy ( $img );
		} else {
			return false;
		}
	}
	
/*
	 * From old rent
	 
	function img_resize($src, $dest, $width=470, $height=310) {
		$rgb=0xFFFFFF;
		$quality=100;

		if (!file_exists($src)) return false;
		$size = getimagesize($src);
		if ($size === false) return false;
		$format = strtolower(substr($size['mime'], strpos($size['mime'], '/')+1));
		$icfunc = "imagecreatefrom" . $format;
		if (!function_exists($icfunc)) return false;
		
		$x_ratio = $size[0]/$width;
		$y_ratio = $size[1]/$height;
		$ratio = min($x_ratio, $y_ratio);
		
		$use_x_ratio = ($x_ratio == $ratio);
		$use_y_ratio = ($y_ratio == $ratio);
		
		$div_x = $use_x_ratio ? 0 : floor(($size[0] - $width * $ratio)/2);
		$div_y = $use_y_ratio ? 0 : floor(($size[1] - $height * $ratio)/2);
		
		

		$isrc = $icfunc($src);

		$idest = imagecreatetruecolor($width, $height);
		imagefill($idest, 0, 0, $rgb);
		imagecopyresampled($idest, $isrc, 0, 0, $div_x, $div_y, $width, $height, $size[0]-2*$div_x, $size[1]-2*$div_y);
		imagejpeg($idest, $dest, $quality);
		imagedestroy($isrc);
		imagedestroy($idest);
		return true;
	}
	
	
	function img_resize_without_crop($src, $dest, $dead_width=900, $dead_height=400) {
		$rgb=0xFFFFFF;
		$quality=100;

		if (!file_exists($src)) return false;
		$size = getimagesize($src);
		if ($size === false) return false;
		$format = strtolower(substr($size['mime'], strpos($size['mime'], '/')+1));
		$icfunc = "imagecreatefrom" . $format;
		if (!function_exists($icfunc)) return false;

		if($size[0] <= $dead_width && $size[1] <= $dead_height)
			return true;
			
		$x_ratio = $size[0]/$dead_width;
		$y_ratio = $size[1]/$dead_height;
		$ratio = max($x_ratio, $y_ratio);
		
		$width = floor($size[0] / $ratio);
		//print $width."<br />";
		$height = floor($size[1] / $ratio);
		//print $height."<br />";
		$isrc = $icfunc($src);

		$idest = imagecreatetruecolor($width, $height);
		imagefill($idest, 0, 0, $rgb);
		imagecopyresampled($idest, $isrc, 0, 0, 0, 0, $width, $height,$size[0], $size[1]);
		imagejpeg($idest, $dest, $quality);
		imagedestroy($isrc);
		imagedestroy($idest);
		return true;
	}
	 */
}

?>